package com.sbw;

import ezvcard.VCard;
import ezvcard.VCardVersion;
import ezvcard.io.text.VCardWriter;
import ezvcard.property.Email;
import ezvcard.property.Telephone;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class VCardUtil {

    public static VCard getVCardByUser(UserInfo userInfo){
        VCard vcard = new VCard();
        Email email = new Email(userInfo.getEmail());
        vcard.addEmail(email);
        vcard.addNote(userInfo.getNote());
        vcard.setFormattedName(userInfo.getUserName());
        Telephone telephone = new Telephone(userInfo.getPhoneNumber());
        vcard.addTelephoneNumber(telephone);
        return vcard;
    }

    public static void write2File(File outFile, List<VCard> vCards) throws IOException {
        VCardWriter writer = new VCardWriter(outFile, VCardVersion.V3_0);
        vCards.forEach(v ->{
            try {
                writer.write(v);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        writer.close();
    }

}
